package com.imy.rootserver.gesture;

import android.util.Log;
import android.view.MotionEvent;

import androidx.test.uiautomator.UiDevice;

import com.imy.rootserver.bean.GesturePoint;
import com.imy.rootserver.helper.MotionEventHelper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GestureHandler implements GestureInterface {

  private String TAG = "GestureHandler";

  private ExecutorService executorService;


  private InteractionController interactionController;

  public GestureHandler(InteractionController interactionController) {
    this.interactionController = interactionController;
    executorService = Executors.newSingleThreadExecutor();

  }

  @Override
  public void down(GesturePoint point) {
    Log.i(TAG,"down:"+point.toString());
    final MotionEvent event = MotionEventHelper.get().createEvent(point);
    EventSendThread obtain = EventSendThread.obtain(event, point.id,interactionController);
    executorService.execute(obtain);
    //if (event!=null){
    //  boolean b = interactionController.injectEventSync(event);
    //  Log.i(TAG,"down:"+b);
    //}

  }

  @Override
  public void up(GesturePoint point) {
    MotionEvent event = MotionEventHelper.get().releasedMotionEvent(point);
    Log.i(TAG,"up:"+point.toString());
    EventSendThread obtain = EventSendThread.obtain(event, point.id,interactionController);
    executorService.execute(obtain);
  }

  @Override
  public void move(GesturePoint point) {
    MotionEvent event = MotionEventHelper.get().onMove(point);
    Log.i(TAG,"onMove event:"+event);
    EventSendThread obtain = EventSendThread.obtain(event, point.id,interactionController);
    executorService.execute(obtain);
  }

  @Override
  public void releaseAll() {

  }

}
